package com.example.administrator.liveapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.adapter.BookingListPageAdapter;
import com.example.administrator.liveapp.bean.ActivityListBean;

import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.view.FullyLinearLayoutManager;
import com.google.gson.Gson;

import org.xutils.common.util.LogUtil;

/**
 * 改版前活动activity(已经没有用了)
 */

public class BookingListPage extends AppCompatActivity {

    private RecyclerView activity_list_recycle;
    private RelativeLayout activity_search_relative;
    private ActivityListBean bean;
    private View loadView;//loding页面（加载页）
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
        data();
        Onclick();
    }
    private void initView() {
        setContentView(R.layout.activity_booking_list_page);
        loadView = findViewById(R.id.loadView);
        activity_list_recycle = (RecyclerView) findViewById(R.id.activity_list_recycle);
        activity_search_relative = (RelativeLayout) findViewById(R.id.activity_search_relative);
    }

    private void data() {
        String url = AppUrl.URLS+AppUrl.ActivityListURL;
        XUtils.Get(url,null,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                bean = gson.fromJson(result,ActivityListBean.class);
                recyclerView();
                loadView.setVisibility(View.GONE);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                LogUtil.e(ex.getMessage()+"联网请求失败");
            }
        });
    }

    private void recyclerView() {
        FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(this);
        linearLayoutManager.setScrollEnabled(true);
        activity_list_recycle.setHasFixedSize(true);
        activity_list_recycle.setLayoutManager(linearLayoutManager);
        BookingListPageAdapter adapter = new BookingListPageAdapter(bean.getActivity());
        activity_list_recycle.setAdapter(adapter);
        adapter.setOnItemClickListener(new BookingListPageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (ApplicationUtils.getInstance().loginBean == null) {
                    Intent intent = new Intent();
                    intent.setClass(BookingListPage.this, WxLoginPage.class);
                    startActivity(intent);
                    BookingListPage.this.overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                    return;
                }
                    Intent intent = new Intent();
//                    intent.putExtra("timeStatus",bean.getActivity().get(position).getTimeStatus()+"");
//                    intent.putExtra("activityId",bean.getActivity().get(position).getActivityid());
                    intent.putExtra("consumerId",ApplicationUtils.getInstance().loginBean.getConsumerid());
                    intent.setClass(BookingListPage.this, BookingDetailsPage.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
            }
        });
    }

    private void Onclick() {
        activity_search_relative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BookingListPage.this,SearchPage.class);
                intent.putExtra("searchTid","2");
                startActivity(intent);
                overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
            }
        });
    }
}
